package TakeAwaySystem.businessoperation.food;

import TakeAwaySystem.Business;
import TakeAwaySystem.businessoperation.BusinessIOperatition;
import TakeAwaySystem.Food;
import TakeAwaySystem.User;

import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Scanner;

import static TakeAwaySystem.Food.FOODEXPLAIN;
import static TakeAwaySystem.Food.FOODPRICE;
import static TakeAwaySystem.User.NAME;

/**
 * @author Vv
 * @version 1.0
 * @project coding_learn
 * @description
 * @date 2023/9/26 22:27:24
 */
public class BusinessModdifyFoodOperation implements BusinessIOperatition {
//    @Override
//    public void work(Business o, File file) {
//        Scanner scanner = new Scanner(System.in);
//        List<Food> ALLFood = Food.fileToList(Food.file);
//        List<Food> myFood =new ArrayList<>();
//        for(Food food : ALLFood){
//            if(food.getBusinessId() == o.getId()){
//                myFood.add(food);
//            }
//        }
//        // 先选要修改哪个商品
//        // 再选要修改什么
//        int N = myFood.size();
//        String str = null;
//        for(int i = 0;i<N;i++){
//            System.out.println("你是否要修改这个商品 ： " + myFood.get(i).getName());
//            System.out.println("如果选择修改这个商品就再键盘输入YES");
//            str = scanner.next();
//            if(!str.equals("YES")){
//                continue;
//            }
//
//            str = "";
//            int choice = 0;
//            Food food = myFood.get(i);
//            do{
//                choice = PrintFoodMofidyObject();
//                switch (choice){
//                    case NAME:
//                    {
//                        String newName = null;
//                        System.out.println("现在的名字是 " + food.getName());
//                        System.out.println("现在请输入你要修改的名字 注意不可以有空格");
//                        //可以修改为之前一样的名字
//                        newName = getNewNameOrExplain();
//                        food.setName(newName);
//                    }
//                    break;
//                    case FOODEXPLAIN :
//                    {
//                        String newExplain = null;
//                        System.out.println("现在的介绍是 " + food.getFoodExplain());
//                        System.out.println("现在请输入你要修改的介绍 注意不能有空格");
//                        newExplain = getNewNameOrExplain();
//                        food.setFoodExplain(newExplain);
//                    }
//                    break;
//                    case FOODPRICE :
//                    {
//                        double newPrice = 0;
//                        System.out.println("现在的价格是 " + food.getFoodPrice());
//                        System.out.println("请输入你要修改的价格 注意不要输入负数或除数字意外其他字母");
//                        newPrice = scanner.nextDouble();
//                        food.setFoodPrice(newPrice);
//                    }
//                    break;
//                }
//                System.out.println("是否要继续修改这个Food的信息 YES/else");
//                str = scanner.next();
//            }while(str.equals("YES"));
//
//            System.out.println("如果已经完成修改任务想要退出 就 输入 EXIT 想要继续修改商品就输入Keep");
//            if(scanner.next().equals("EXIT")){
//                break;
//            }
//        }
//        Food.ListToFoodFile(ALLFood,file);
//    }
//    private String getNewNameOrExplain(){
//        Scanner scanner = new Scanner(System.in);
//        String str = null;
//        do{
//             str = scanner.next();
//        }while(str.contains(" "));
//        return str;
//    }
    public int PrintFoodMofidyObject(){
        Scanner scanner = new Scanner(System.in);
        System.out.println("如果要修改商品的名字 请输入1");
        System.out.println("如果要修改商品的介绍 请输入2");
        System.out.println("如果要修改商品的价格 请输入3");
        return scanner.nextInt();
    }


    @Override
    public void work(Business o, File file) {
        Scanner scanner = new Scanner(System.in);
        List<Food> foodList = Food.fileToList(file);
        List<Food> myFood = new ArrayList<>();
        for(Food food : foodList){
            if(food.getBusinessId() == o.Id){
                myFood.add(food);
            }
        }
        do{
            System.out.println("选择要改变信息的食物");
            Food choiseFood = printTochoiseFood(myFood);
            System.out.println("你选择的是 " + choiseFood.getName());
            int choise = PrintFoodMofidyObject();
            switch(choise) {
                case NAME: {
                    String newName = null;
                    do {
                        System.out.println("名字中不允许出现空格");
                        newName = scanner.next();
                        if (newName.contains(" ")) {
                            System.out.println("名字中含有空格");
                        }
                    } while (newName.contains(" "));
                    choiseFood.setName(newName);
                }
                System.out.println("修改完成");
                break;
                case FOODEXPLAIN: {
                    String newExplain = null;
                    do {
                        System.out.println("名字中不允许出现空格");
                        newExplain = scanner.next();
                        if (newExplain.contains(" ")) {
                            System.out.println("名字中含有空格");
                        }
                    } while (newExplain.contains(" "));
                    choiseFood.setFoodExplain(newExplain);
                }
                System.out.println("修改完成");
                break;
                case FOODPRICE: {
                    double newPrice = 0;
                    System.out.println("现在的价格是 " + choiseFood.getFoodPrice());
                    System.out.println("请输入你要修改的价格 注意不要输入负数或除数字意外其他字母");
                    newPrice = scanner.nextDouble();
                    choiseFood.setFoodPrice(newPrice);
                }
                System.out.println("修改完成");
                break;
            }
            System.out.println("如果已经完成修改任务想要退出 就 输入 EXIT 想要继续修改商品就输入Keep");
        }while(!scanner.next().equals("EXIT"));
        Food.ListToFoodFile(foodList,file);
    }
    private Food printTochoiseFood(List<Food> myFood){
        HashMap<Integer,Food> hashMap = new HashMap<>();
        int N = myFood.size();
        Scanner scanner = new Scanner(System.in);
        for(int i = 0;i<N;i++){
            System.out.println((i+1) + " . " + myFood.get(i).getName() + " " + myFood.get(i).getFoodExplain() + " " + myFood.get(i).getFoodPrice());
            hashMap.put(i+1,myFood.get(i));
        }
        return hashMap.get(scanner.nextInt());
    }

    @Override
    public void ListToFile(List<User> list,File file) {
        // File file = new File("d:/Business.txt");
        FileWriter fileWriter = null;
        BufferedWriter bw = null;
        try {
            fileWriter = new FileWriter(file);
            bw = new BufferedWriter(fileWriter);
            int N = list.size();
            for(int i = 0;i<N;i++){
                Business user = (Business) list.get(i);
                StringBuilder sbr = new StringBuilder();
                sbr.append(user.getId() + " ");
                sbr.append(user.getPassword() + " ");
                sbr.append(user.getName() + " ");
                sbr.append(user.getBusinessAddress() + " ");
                sbr.append(user.getBusinessExplain() + " ");
                sbr.append(user.getStarPrice() + " ");
                sbr.append(user.getDeliveryPrice());
                bw.write(sbr.toString());
                bw.newLine();
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }finally {
            try {
                bw.close();
                fileWriter.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }

        }

    }


    @Override
    public List<User> fileToList(File file)  {
        List<User> list = new ArrayList<>();
        // File file = new File("d:/Business.txt");
        FileReader fr = null;
        BufferedReader br = null;
        try {
            fr = new FileReader(file);
            br = new BufferedReader(fr);
            while(true){
                String str = br.readLine();
                if(str != null){
                    String[] arr = str.split(" ");
                    Business users = new Business();
                    users.setId(Integer.valueOf(arr[0]));
                    users.setPassword(arr[1]);
                    users.setName(arr[2]);
                    users.setBusinessAddress(arr[3]);
                    users.setBusinessExplain(arr[4]);
                    users.setStarPrice(Double.valueOf(arr[5]));
                    users.setDeliveryPrice(Double.valueOf(arr[6]));
                    list.add(users);
                }else{
                    break;
                }
            }
        }catch (IOException e) {
            throw new RuntimeException(e);
        }finally {
            try {
                fr.close();
                br.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }

        return list;
    }
}
